/*
 * @FileName: [RechargecardController.java]
 * 
 * @Package controller
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO Technology Limited Company ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 775 $ $LastChangedDate: 2013-10-12 10:19:36 +0800 (星期六, 12 十月 2013) $ $LastChangedBy: huangb $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: huangb $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bcinfo.iccard.admin.iccard.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bcinfo.iccard.admin.common.ModuleEnum;
import com.bcinfo.iccard.admin.controller.AbstractController;
import com.bcinfo.iccard.admin.util.DataMatrixUtil;
import com.bcinfo.iccard.exception.ComboCardNameIsExistException;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.exception.IccardException;
import com.bcinfo.iccard.model.CheckAccountLog;
import com.bcinfo.iccard.model.ComboCard;
import com.bcinfo.iccard.model.ComboCardCheckreason;
import com.bcinfo.iccard.model.ComboCardSaleDetail;
import com.bcinfo.iccard.model.Merchant;
import com.bcinfo.iccard.service.ComboCardService;
import com.bcinfo.iccard.util.PaginationList;
import com.bcinfo.wapportal.util.Encryption;

/**
 * 套餐卡controller <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 包含以下功能 <br/>
 * <br/>
 * 
 * <PRE>
 *      /iccard/${actionName}
 * </PRE>
 * <p>
 */
@Controller
@RequestMapping("/iccard/combocard")
public class ComboCardController extends AbstractController
{
    @Autowired()
    private ComboCardService     comboCardService;

    
    /*
     * (non-Javadoc) Description: <br/>
     * 
     * @return
     * 
     * @see com.bcinfo.upp.admin.controller.AdminController#getMouduleCode()
     */
    public String getMouduleCode()
    {
        return ModuleEnum.ComboCard.moduleName.value;
    }
    /**
     * 转到ComboCard列表页面 <br/>
     * <p>
     * Description: 转到ComboCard列表页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:56:22<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/toListView")
    public ModelAndView toListView()
    {
        String cardNum = request.getParameter("cardNum") == null ? "" : request.getParameter("cardNum");
        request.setAttribute("cardNum", cardNum);
        return new ModelAndView("iccard/combo/combocard_list");
    }

    /**
     * 查询ComboCard列表 <br/>
     * <p>
     * Description: 查询ComboCard列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(String cardName,int status)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<ComboCard> list = null;

        try
        {
            list = comboCardService.find(cardName, status, getPageSize(),getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }
    
    /**
     * 验证套餐卡名称 <br/>
     * <p>
     * Description: 验证套餐卡名称<br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:58:32<br/>
     * <p>
     * 
     * @param request
     * @param response
     * @return
     * 
     */
    @RequestMapping("/checkCardName")
    @ResponseBody
    public boolean checkCardName(String cardName)
    {
        boolean bl = false;
        try
        { 
            int count = comboCardService.checkCardName(cardName);
            if(count>0){
                bl = true;
            }
        }
        catch (IccardDatabaseException e)
        {
            handleException(e);
        }
        catch (Exception e)
        {
            handleException(e);
        }
        return bl;
    }
    
    /**
     * 创建ComboCard信息 <br/>
     * <p>
     * Description: 创建ComboCard信息 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:58:32<br/>
     * <p>
     * 
     * @param request
     * @param response
     * @return
     * 
     */
    @RequestMapping("/add")
    @ResponseBody
    public Map<String, String> add(ComboCard comboCard)
    {
        System.out.println(comboCard.toString());
        comboCard.setBegintime(request.getParameter("begintime"));
        comboCard.setEndtime(request.getParameter("begintime"));
        comboCard.setComboCardSurplusCopies(comboCard.getComboCardTotalCopies());
        comboCard.setIsDelete(true);
//        if (!this.hasPermission(ModuleEnum.ComboCard.add))// 没有权限
//        {
//            _log.error("权限不足");
//            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
//        }
        Map<String, String> map = null;
        try
        { 
            comboCardService.add(comboCard);
            
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.ComboCard.add, "comboCard=" + comboCard.toString() + "|desc=新增套餐卡成功");
        }
        catch (ComboCardNameIsExistException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "套餐卡名称已存在，新增套餐卡失败。");
            //addLog(ModuleEnum.ComboCard.add, "套餐卡名称已存在，新增套餐卡失败。");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            //addLog(ModuleEnum.ComboCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "新增套餐卡失败");
            //addLog(ModuleEnum.ComboCard.add, "新增套餐卡失败");
        }
        return map;
    }

    /**
     * 转到新增页面 <br/>
     * <p>
     * Description: 转到新增页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toAddView")
    public ModelAndView toAddView()
    {
        ModelAndView mv = new ModelAndView("iccard/combo/combocard_add");
        mv.addObject("nextaction", "add");
        mv.addObject("formtitle", "新增");
        return mv;
    }


    /**
     * 删除套餐卡 <br/>
     * <p>
     * Description: 删除套餐卡 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param comboCard
     * @return
     * 
     */
    @RequestMapping("/remove")
    @ResponseBody
    public Map<String, String> remove(long comboCardId)
    {
//        if (!this.hasPermission(ModuleEnum.ComboCard.remove))// 没有权限
//        {
//            _log.error("权限不足");
//            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
//        }
        Map<String, String> map = null;
        if (comboCardId <= 0)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", null);
        }
        try
        {
            comboCardService.delete(comboCardId);
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.ComboCard.remove, "ComboCardId=" + comboCardId + "|desc=删除套餐卡成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            //addLog(ModuleEnum.ComboCard.remove, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            //addLog(ModuleEnum.ComboCard.remove, "删除套餐卡失败");
        }
        return map;
    }
    
    /**
     * 转到编辑页面 <br/>
     * <p>
     * Description: 转到编辑页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toEditView")
    public ModelAndView toEditView()
    {

        ModelAndView mv = new ModelAndView("iccard/combo/combocard_add");
//        if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
//        {
//            mv.addObject("updateCode", ModuleEnum.ComboCard.update);
//            return null;
//        }
        try
        {
            ComboCard r = comboCardService.load(Long.parseLong((String)request.getParameter("comboCardId")));
            mv.addObject("comboCard", r);
        }
        catch (IccardDatabaseException e)
        {
            handleException(e);
        }
        mv.addObject("nextaction", "update");
        mv.addObject("formtitle", "编辑");

        return mv;
    }
    
    /**
     * 编辑套餐卡 <br/>
     * <p>
     * Description:  <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param comboCard
     * @return
     * 
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, String> update(ComboCard comboCard)
    {
//        if (!this.hasPermission(ModuleEnum.ComboCard.remove))// 没有权限
//        {
//            _log.error("权限不足");
//            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
//        }
        comboCard.setBegintime(request.getParameter("begintime"));
        comboCard.setEndtime(request.getParameter("endtime"));
        comboCard.setComboCardCheckStatus((short) 0);
        comboCard.setComboCardSurplusCopies(comboCard.getComboCardTotalCopies());
        System.out.println(comboCard.toString());
        
        Map<String, String> map = null;
        if (comboCard == null || comboCard.getComboCardId() <= 0)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", "编辑失败");
        }
        try
        {
            comboCardService.update(comboCard);
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.ComboCard.update, "comboCard=" + comboCard.toString() + "|desc=套餐卡编辑成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            //addLog(ModuleEnum.ComboCard.update, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            //addLog(ModuleEnum.ComboCard.update, "编辑失败");
        }
        return map;
    }
    
    /**
     * 查询商家列表 <br/>
     * <p>
     * Description: 查询商家列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/merchantlist")
    @ResponseBody
    public List<Merchant> findAllMerchant()
    {
        List<Merchant> list = null;
        try
        {
            list = comboCardService.findAllMerchant();
        }
        catch (IccardDatabaseException e)
        {
            e.printStackTrace();
            handleException(e);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            handleException(e);
        }
        return list;
    }
    
    /**
     * 转到编辑页面 <br/>
     * <p>
     * Description: 转到编辑页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toAuditView")
    public ModelAndView toAuditView()
    {

        ModelAndView mv = new ModelAndView("iccard/combo/combocard_audit");
//        if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
//        {
//            mv.addObject("updateCode", ModuleEnum.ComboCard.update);
//            return null;
//        }
        try
        {
            ComboCard r = comboCardService.load(Long.parseLong((String)request.getParameter("comboCardId")));
            mv.addObject("comboCard", r);
        }
        catch (IccardDatabaseException e)
        {
            handleException(e);
        }
        mv.addObject("nextaction", "audit");
        mv.addObject("formtitle", "审核");

        return mv;
    }
    
    /**
     * 编辑套餐卡 <br/>
     * <p>
     * Description:  <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param comboCard
     * @return
     * 
     */
    @RequestMapping("/audit")
    @ResponseBody
    public Map<String, String> audit(long comboCardId,int status,String msg)
    {
//        if (!this.hasPermission(ModuleEnum.ComboCard.remove))// 没有权限
//        {
//            _log.error("权限不足");
//            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
//        }
        
        Map<String, String> map = null;
        if (comboCardId <= 0 && status<1 && status>2)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", "审核失败");
        }
        try
        {
            comboCardService.audit(comboCardId,status,msg,getOperatorId());
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.ComboCard.audit, "comboCardId=" + comboCardId + "|desc=审核套餐卡成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            //addLog(ModuleEnum.ComboCard.audit, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            //addLog(ModuleEnum.ComboCard.audit, "审核失败");
        }
        return map;
    }
    
    
    /**
     * 转到销售页面 <br/>
     * <p>
     * Description: 转到 销售页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toSaleView")
    public ModelAndView toSaleView()
    {

        ModelAndView mv = new ModelAndView("iccard/combo/combocard_sale");
//        if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
//        {
//            mv.addObject("updateCode", ModuleEnum.ComboCard.update);
//            return null;
//        }
        try
        {
            ComboCard r = comboCardService.load(Long.parseLong((String)request.getParameter("comboCardId")));
            mv.addObject("comboCard", r);
        }
        catch (IccardDatabaseException e)
        {
            handleException(e);
        }
        mv.addObject("nextaction", "slae");
        mv.addObject("formtitle", "销售");

        return mv;
    }
    
    /**
     * 销售套餐卡 <br/>
     * <p>
     * Description:  <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-18-下午2:09:07<br/>
     * <p>
     * 
     * @param comboCard
     * @return
     * 
     */
    @RequestMapping("/sale")
    @ResponseBody
    public Map<String, String> sale(long comboCardId,int status)
    {
//        if (!this.hasPermission(ModuleEnum.ComboCard.remove))// 没有权限
//        {
//            _log.error("权限不足");
//            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, null, "权限不足");
//        }
        
        Map<String, String> map = null;
        if (comboCardId <= 0 && status<0 && status>1)
        {
            return packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR,
                    "InvalidParameterException", "销售套餐卡失败");
        }
        try
        {
            String twoDimensionCode = ""+comboCardId+ System.currentTimeMillis() +DataMatrixUtil.randomNumber();
            int width = 200;
            int height = 200;
            String pwdcode=Encryption.encrypt(new StringBuffer(twoDimensionCode)).toString();
            com.google.zxing.common.BitMatrix matrix = DataMatrixUtil.encode(pwdcode, width, height);
//            String path = getConfigByKey("qrcodepath");
//            System.out.println("getConfigByKey(qrcodepath)="+path);
            
            String imgName = System.currentTimeMillis() +DataMatrixUtil.randomNumber()+".png";
            String strQRCodePath = request.getSession().getServletContext().getRealPath("/")+"\\qrimg\\"+imgName;
            
            String url = "/qrimg/"+imgName;
            DataMatrixUtil.writeToFile(matrix, "jpg", strQRCodePath);
            ComboCardSaleDetail comboCardSaleDetail =comboCardService.sale(comboCardId,status,getOperatorId(),twoDimensionCode,url);
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            map.put("comboCardSaleId", comboCardSaleDetail.getComboCardSaleId().toString());
            addLog(ModuleEnum.ComboCard.sale, "comboCardId=" + comboCardId + "|desc=销售套餐卡成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            //addLog(ModuleEnum.ComboCard.sale, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), null);
            addLog(ModuleEnum.ComboCard.sale, "销售套餐卡失败");
            //e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 转到销售二维码页面 <br/>
     * <p>
     * Description: 转到 销售二维码页面 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toQRView")
    public ModelAndView toQRView()
    {

        ModelAndView mv = new ModelAndView("iccard/combo/combocard_QR");
//        if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
//        {
//            mv.addObject("updateCode", ModuleEnum.ComboCard.update);
//            return null;
//        }
        try
        {
            ComboCardSaleDetail d = comboCardService.loadComboCardSaleDetail(Long.parseLong((String)request.getParameter("comboCardSaleId")));
            mv.addObject("comboCardSaleDetail", d);
            ComboCard r = comboCardService.load(Long.parseLong((String)request.getParameter("comboCardId")));
            mv.addObject("comboCard", r);
        }
        catch (IccardDatabaseException e)
        {
            handleException(e);
        }
        mv.addObject("nextaction", "qr");
        mv.addObject("formtitle", "销售二维码");

        return mv;
    }

    /**
     * 查询ComboCard列表 <br/>
     * <p>
     * Description: 查询ComboCard列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/qrlist")
    @ResponseBody
    public Map<String, Object> qrlist(String twoDimensionCode)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<ComboCardSaleDetail> list = null;

        try
        {
            list = comboCardService.findComboCardSaleDetails(twoDimensionCode, getPageSize(),getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }
    
    /**
     * 转到套餐卡详情 <br/>
     * <p>
     * Description: 转到套餐卡详情 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toInfoView")
    public ModelAndView toInfoView()
    {

        ModelAndView mv = new ModelAndView("iccard/combo/combocard_info");
//        if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
//        {
//            mv.addObject("updateCode", ModuleEnum.ComboCard.update);
//            return null;
//        }
        try
        {
            ComboCardCheckreason d = comboCardService.loadComboCardCheckreason(Long.parseLong((String)request.getParameter("comboCardId")));
            mv.addObject("comboCardCheckreason", d);
            ComboCard r = comboCardService.load(Long.parseLong((String)request.getParameter("comboCardId")));
            mv.addObject("comboCard", r);
        }
        catch (IccardDatabaseException e)
        {
            handleException(e);
        }
        mv.addObject("nextaction", "info");
        mv.addObject("formtitle", "套餐卡详情");

        return mv;
    }
    
    
    /**
     * 转到套餐卡提取详情 <br/>
     * <p>
     * Description: 转到套餐卡提取详情 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-4-17-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toGetInfoView")
    public ModelAndView toGetInfoView()
    {

        ModelAndView mv = new ModelAndView("iccard/combo/combocard_getlist");
//        if (!this.hasPermission(ModuleEnum.ComboCard.update))// 没有权限
//        {
//            mv.addObject("updateCode", ModuleEnum.ComboCard.update);
//            return null;
//        }
        String comboCardId = request.getParameter("comboCardId") == null ? "" : request.getParameter("comboCardId");
        request.setAttribute("comboCardId", comboCardId);
        mv.addObject("nextaction", "getinfo");
        mv.addObject("formtitle", "套餐卡提取详情");

        return mv;
    }
    
    /**
     * 查询提取详情列表 <br/>
     * <p>
     * Description: 查询提取详情列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/getlist")
    @ResponseBody
    public Map<String, Object> getlist(long comboCardId,int status)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<ComboCardSaleDetail> list = null;
        try
        {
            list = comboCardService.findGetInfo(comboCardId, status, getPageSize(),getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }
        return map;
    }
    
    /**
     * 查询套餐卡对账列表 <br/>
     * <p>
     * Description: 查询套餐卡对账列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/accountlist")
    @ResponseBody
    public Map<String, Object> accountlist(long opId)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<CheckAccountLog> list = null;

        try
        {
            list = comboCardService.findCheckAccount(opId,getPageSize(),getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }
    
    
    /**
     * 查询套餐卡对账列表 <br/>
     * <p>
     * Description: 查询套餐卡对账列表 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/accountinfo")
    @ResponseBody
    public Map<String, Object> accountinfo(long saleUserId,int status)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        PaginationList<ComboCardSaleDetail> list = null;

        try
        {
            list = comboCardService.findAccountInfo(saleUserId, status,getPageSize(),getPage());
            if (list != null)
            {
                map.put("rows", list.getRecords());
                map.put("total", list.getTotalRecord());
            }
        }
        catch (IccardException e)
        {
            map.put("total", 0);
            handleException(e);
        }
        catch (Exception e)
        {
            map.put("total", 0);
            handleException(e);
        }

        return map;
    }
    
    /**
     * 转到对账详细页面 <br/>
     * <p>
     * Description: 转到对账详细页面  <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-上午11:30:01<br/>
     * <p>
     * 
     * @return ModelAndView
     * 
     */
    @RequestMapping("/toAccountView")
    public ModelAndView toAccountView()
    {
        ModelAndView mv = new ModelAndView("iccard/rcreconciliation/combocard_account_info");
        String userId = request.getParameter("userId") == null ? "" : request.getParameter("userId");
        String accountDate = request.getParameter("accountDate") == null ? "" : request.getParameter("accountDate");
        request.setAttribute("userId", userId);
        request.setAttribute("accountDate", accountDate);
        return mv;
    }
    
    /**
     * 套餐卡对账 <br/>
     * <p>
     * Description: ComboCard对账 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-20-下午2:58:32<br/>
     * <p>
     * 
     * @param request
     * @param response
     * @return
     * 
     */
    @RequestMapping("/account")
    @ResponseBody
    public Map<String, String> account(String recentlyAccountDate,float accountMount,long saleUserId)
    {
        Map<String, String> map = null;
        try
        {
            Date d=null;
            if(StringUtils.isNotBlank(recentlyAccountDate) && recentlyAccountDate.equals("null")){
                d = new Date(Long.parseLong(recentlyAccountDate));
            }
            System.out.println(d);
            comboCardService.account(d,accountMount,saleUserId,getOperatorId());
            
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_SUCCESS, ERROR_VALUE_MSG_TYPE_INFO,
                    ERROR_VALUE_OP_RESULT_SUCCESS_NAME, null);
            addLog(ModuleEnum.ComboCard.add, "saleUserId=" + saleUserId + "|desc=套餐卡对账成功");
        }
        catch (IccardDatabaseException e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "数据库错误");
            //addLog(ModuleEnum.ComboCard.add, "数据库错误");
        }
        catch (Exception e)
        {
            map = packageAjaxReturnMap(ERROR_VALUE_OP_RESULT_FAIL, ERROR_VALUE_MSG_TYPE_ERROR, e.getClass()
                    .getSimpleName(), "套餐卡对账失败");
            //addLog(ModuleEnum.ComboCard.add, "套餐卡对账失败");
        }
        return map;
    }
    
    /**
     * 查询销售人员销售额 <br/>
     * <p>
     * 查询销售人员销售额 <br/>
     * <p>
     * Author: huangb<br/>
     * <p>
     * Date: 2013-12-23-下午2:57:52<br/>
     * <p>
     * 
     * @return
     * 
     */
    @RequestMapping("/findall")
    @ResponseBody
    public float findall(long saleUserId,int status)
    {
        float f = 0;

        try
        {
            f = comboCardService.findALLAccountBySaleUserId(saleUserId,status);
        }
        catch (IccardException e)
        {
            e.printStackTrace();
            handleException(e);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            handleException(e);
        }
        return f;
    }
}